package com.example.smartcitymovie.ui.dashboard;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.SearchView;
import androidx.fragment.app.Fragment;

import com.example.smartcitymovie.MyApplication;
import com.example.smartcitymovie.R;
import com.example.smartcitymovie.adapter.RecycleViewAdapter;
import com.example.smartcitymovie.bean.AreaThreaterBean;
import com.example.smartcitymovie.bean.LocalAddressBean;
import com.example.smartcitymovie.dataRepo.ApiData;
import com.example.smartcitymovie.databinding.FragmentDashboardBinding;
import com.example.smartcitymovie.ui.movie.ChooseTharterTimes;
import com.example.smartcitymovie.utils.OkHttpUtils;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class DashboardFragment extends Fragment {

    private FragmentDashboardBinding binding;
    private AreaThreaterBean areaThreaterBean;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentDashboardBinding.inflate(inflater, container, false);
        setHasOptionsMenu(true);
        return binding.getRoot();
    }

    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.adress_menu, menu);
    }

    @Override
    public void onPrepareOptionsMenu(@NonNull Menu menu) {
        super.onPrepareOptionsMenu(menu);
        MenuItem item = menu.findItem(R.id.address_menu);
        LocalAddressBean localAddressBean = ApiData.getLocalAddressBean();
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (item != null) {
                        item.setTitle(localAddressBean.getData().getLocation());
                        LoadLocalTheater(localAddressBean);
                        SearchTheater();
                    }
                }
            });
    }

    /*
     * 搜索影院
     * */
    private void SearchTheater() {
    }

    /*
     * 加载本区域所有影院列表
     * */
    private void LoadLocalTheater(LocalAddressBean localAddressBean) {
        areaThreaterBean = ApiData.getAreaThreaterBean();
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                    binding.RVTheaterList.setLinearLayoutManager(LinearLayout.VERTICAL);
                    binding.RVTheaterList.setAdapter(areaThreaterBean.getRows(), R.layout.area_threater_list_item_layout);
                    binding.RVTheaterList.getBaseAdapter().setOnRecycleViewItemCLickListener(new RecycleViewAdapter.OnRecycleViewItemCLickListener() {
                        @Override
                        public void onRecycleViewItemCLickListener(View view, int position) {
                            Intent intent = new Intent(getActivity(), ChooseTharterTimes.class);
                            intent.putExtra("tharterId",areaThreaterBean.getRows().get(position).getId());
                            startActivity(intent);
                        }
                    });
                }
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}